package com.fanhao.dllo.justforyou.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fanhao.dllo.justforyou.R;

/**
 * Created by dllo on 15/10/22.
 */
public class PictureActivity extends Activity {


    private ViewPager pictureViewPager;
    private ImageView[] tips; // 创建一个提示性点数组
    private int[] images; // 图片id数组
    private int currentPage = 0; // 当前展示的页码

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);

        initView();
    }

    // 初始化控件
    public void initView() {
        pictureViewPager = (ViewPager) findViewById
                (R.id.pictureViewPage);

        // 存放点点的容器
        LinearLayout tipsBox = (LinearLayout) findViewById
                (R.id.tipsBox);

        // 初始化图片资源
        images = new int[]{R.mipmap.third, R.mipmap.first, R.mipmap.second};

        // 初始化点点
        tips = new ImageView[3];
        for (int i = 0; i < tips.length; i++) {
            ImageView img = new ImageView(this);
            ViewGroup.LayoutParams lp = img.getLayoutParams();
            img.setLayoutParams(new ViewGroup.LayoutParams(40, 40));

            tips[i] = img;
            if (i == 0) {
                img.setBackgroundResource(R.mipmap.page_now);// 蓝色背景
            } else {
                img.setBackgroundResource(R.mipmap.page); // 白色背景
            }

            LinearLayout.LayoutParams params = new
                    LinearLayout.LayoutParams(new
                    ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            params.leftMargin = 3;
            params.rightMargin = 3;
            tipsBox.addView(img);// 把点点添加到容器中

        }

        // 初始化PagerAdapter
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return images.length;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(ViewGroup container,
                                    int position, Object object) {
                // container.removeViewAt(position);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView im = new ImageView(PictureActivity.this);
                im.setScaleType(ImageView.ScaleType.FIT_XY);
                im.setImageResource(images[position]);
                container.addView(im);
                return im;
            }
        };

        pictureViewPager.setAdapter(adapter);


        // 更改当前tip
        pictureViewPager.setOnPageChangeListener(new
          ViewPager.OnPageChangeListener() {

          @Override
          public void onPageScrollStateChanged(int arg0) {

           }

          @Override
          public void onPageScrolled(int position, float positionOffset,
                                     int positionOffsetPixels) {

          }

           @Override
           public void onPageSelected(int position) {
           Log.i("fanhao", String.valueOf(position));
               for (ImageView tip : tips) {
                   tip.setBackgroundResource(R.mipmap.page);
               }

//           tips[currentPage].setBackgroundResource(R.mipmap.page);

//           currentPage = position;

           tips[position].setBackgroundResource(R.mipmap.page_now);
           }


           });
    }

}